package com.wyx.Controller;

import com.wyx.Common.Code;
import com.wyx.Common.Result;
import com.wyx.Pojo.Book;
import com.wyx.Service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 图书的视图层
 */
@RestController
@RequestMapping("/books")
public class bookController {
    @Autowired
    private BookService bookService;

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    protected Result findById(@PathVariable Integer id) {
        Integer code;
        String msg;
        if (bookService.selectbyId(id) != null) {
            code = Code.SELECT_ONE_OK;
            msg = "";
        } else {
            code = Code.SELECT_ONE_ERR;
            msg = "图书不存在，请重新查询";
        }
        return new Result(bookService.selectbyId(id), code, msg);
    }

    /**
     * 查询所有
     *
     * @param response
     * @param request
     * @return
     */
    @GetMapping
    protected Result<List<Book>>  findall(HttpServletResponse response, HttpServletRequest request) {
        Integer code;
        String msg;
        if (bookService.selectall() != null) {
            code = Code.SELECT_ALL_OK;
            msg = "";
        } else {
            code = Code.SELECT_ALL_ERR;
            msg = "系统繁忙，请稍后再试";
        }
        return new Result<>(bookService.selectall(), code, msg);
    }

    /**
     * 分页条件查询操作
     * @param name
     * @param writter
     * @param page
     * @param pagesizes
     * @return
     */
    @GetMapping("/page/name={name}&writter={writter}/page={page}&pagesizes={pagesizes}")
    protected Result<List<Book>> getpage(@PathVariable("name") String name, @PathVariable("writter") String writter, @PathVariable("page") int page, @PathVariable("pagesizes") int pagesizes) {

        List<Book> page1 = bookService.getPage(name, writter,page, pagesizes);
        System.out.println(page1);
        Result<List<Book>> res;
        if (page1 != null) {
            res = new Result<>(page1, 101, "查询成功");
        } else {
            res = new Result<>(null, 100, "查询失败");
        }
        return res;
    }

    /**
     * 添加书籍操作
     *
     * @param book
     * @return
     */
    @PostMapping
    protected Result<Book> ADD(@RequestBody Book book) {
        Integer code;
        String msg;
        if (bookService.add(book)) {
            code = Code.ADD_OK;
            msg = "添加成功";
        } else {
            code = Code.ADD_ERR;
            msg = "添加失败，请确定数据是否正确";
        }
        return new Result<>(null, code, msg);
    }

    /**
     * 修改书籍操作
     *
     * @param book
     * @return
     */
    @PutMapping
    protected Result<String> Update(@RequestBody Book book) {
        Integer code;
        String msg;
        if (bookService.update(book)) {
            code = Code.UPDATE_OK;
            msg = "修改成功";
        } else {
            code = Code.UPDATE_ERR;
            msg = "修改失败，请稍后再试";
        }
        return new Result<>(null, code, msg);
    }

    @DeleteMapping("/{id}")
    protected Result<String> Delete(@PathVariable Integer id) {
        Integer code;
        String msg;
        if (bookService.delete(id)) {
            code = Code.DELETE_OK;
            msg = "删除成功";
        } else {
            code = Code.DELETE_ERR;
            msg = "删除失败，请稍后再试";
        }
        return new Result<>(null, code, msg);
    }

    /**
     * 获取书籍总数目
     *
     * @return
     */
    @GetMapping("/getcount/name={name}&writter={writter}")
    protected Integer getcount(@PathVariable("name") String name,@PathVariable("writter") String writter) {

        return bookService.getCount(name, writter);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PutMapping("/deletebyIds")
    protected Result<String> getcount( @RequestBody  Book[]ids) {
        if (null==ids || ids.length==0){
            return new Result<>("",500,"请选择要删除的书籍");
        }
        for (Book id : ids) {
            bookService.delete(id.getId());
        }
        return new Result<>("",501,"") ;
    }
}
